import cv2
import pytesseract

carer = cv2.CascadeClassifier("./haarcascades/haarcascade_russian_plate_number.xml")

img = cv2.imread("../asset/car2.jpeg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cars = carer.detectMultiScale(gray, 1.15, 3)
print(cars)
for (x, y, w, h) in cars:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
roi = gray[y:y + h, x:x + w]
ret, roi_bin = cv2.threshold(roi, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
text = pytesseract.image_to_string(roi_bin, lang='chi_sim+eng', config='--psm 8 --oem 3')
print(text)
cv2.putText(img, text, (20, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
while True:
    cv2.imshow('img', img)
    key = cv2.waitKey(10)
    if key & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()
